﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for MethodClassInmast
/// </summary>
public class MethodClassInmast
{
    ArrayList location;
    private String strconn = BCDataCenter.Configuration.CurrentConnectionString;
    SqlConnection con;
    SqlCommand command, insertInloc,getLocation;
    SqlTransaction tran;
    SqlDataReader dr;
    public MethodClassInmast()
	{
        con = new SqlConnection(strconn);
	}
    void get_data_to_location()
    {
        con.Open();
        location = new ArrayList();
        getLocation = new SqlCommand("GET_LOCATION_INLOCD", con);
        getLocation.CommandType = CommandType.StoredProcedure;
        dr = getLocation.ExecuteReader(CommandBehavior.CloseConnection);
        while (dr.Read())
        {
            location.Add(dr[0].ToString());
        }
        dr.Close();
    }
    public int Update_INMAST(string itemkey, string description, string commodkey, string inclasskey, string productkey, float lastcost, float price, float purchconvfctr, string ConVertDesc, string abckey, string saletaxkey, string date, string image, bool hangbo, string itemkey1, string description1, string SMSCode, string MFkey, string vendorkey, string CaseDesc, string PackageConvert, string PackageConvertDesc, float Volume, float Weight, int costkey, float Itemlength, float Itemwidth, float Itemheight)
    {
            con.Open();
            command = new SqlCommand();
            command.CommandText = ("Update_INMAST");
            command.Connection = con;
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@itemkey", itemkey));
            command.Parameters.Add(new SqlParameter("@CombiPack", hangbo));
            command.Parameters.Add(new SqlParameter("@description", description));
            command.Parameters.Add(new SqlParameter("@itemkey1", itemkey1));
            command.Parameters.Add(new SqlParameter("@description1", description1));
            command.Parameters.Add(new SqlParameter("@SMSCode", SMSCode));
            command.Parameters.Add(new SqlParameter("@abckey", abckey));
            command.Parameters.Add(new SqlParameter("@Itemlength", Itemlength));
            command.Parameters.Add(new SqlParameter("@Itemwidth", Itemwidth));
            command.Parameters.Add(new SqlParameter("@Itemheight", Itemheight));
            command.Parameters.Add(new SqlParameter("@saletaxkey", saletaxkey));
            command.Parameters.Add(new SqlParameter("@commodkey", commodkey));
            command.Parameters.Add(new SqlParameter("@inclasskey", inclasskey));
            command.Parameters.Add(new SqlParameter("@productkey", productkey));
            command.Parameters.Add(new SqlParameter("@MFkey", MFkey));
            command.Parameters.Add(new SqlParameter("@vendorkey", vendorkey));
            command.Parameters.Add(new SqlParameter("@purchconvfctr", purchconvfctr));
            command.Parameters.Add(new SqlParameter("@lastcost", lastcost));
            command.Parameters.Add(new SqlParameter("@CaseDesc", CaseDesc));
            command.Parameters.Add(new SqlParameter("@PackageConvert", PackageConvert));
            command.Parameters.Add(new SqlParameter("@Volume", Volume));
            command.Parameters.Add(new SqlParameter("@costkey", costkey));
            command.Parameters.Add(new SqlParameter("@price", price));
            command.Parameters.Add(new SqlParameter("@ConVertDesc", ConVertDesc));
            command.Parameters.Add(new SqlParameter("@PackageConvertDesc", PackageConvertDesc));
            command.Parameters.Add(new SqlParameter("@Weight", Weight));
            command.Parameters.Add(new SqlParameter("@date", DateTime.Now.Date.ToString()));
            command.Parameters.Add(new SqlParameter("@ImgName", image));
            SqlParameter res = new SqlParameter("@res", SqlDbType.Int);
            res.Direction = ParameterDirection.ReturnValue;
            command.Parameters.Add(res);
            command.ExecuteNonQuery();
            int kq = Convert.ToInt32(res.Value);
            con.Close();
            return kq;
    }
    public DataTable get_data_table(string procedure, string parameter)
    {
        SqlConnection con = new SqlConnection(strconn);
        command = new SqlCommand(procedure, con);
        command.Parameters.Add(new SqlParameter("@itemkey", parameter));
        command.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataTable dtb = new DataTable();
        da.Fill(dtb);
        return dtb;
    }
    int execute()
    {
        SqlParameter res = new SqlParameter("@res", SqlDbType.Int);
        res.Direction = ParameterDirection.ReturnValue;
        command.Parameters.Add(res);
        command.ExecuteNonQuery();
        con.Close();
        int kq = Convert.ToInt32(res.Value);
        return kq;
    }

    public int  insertInMast(string itemkey,string description,string commodkey,string inclasskey,string productkey,float lastcost,float price,float purchconvfctr,string ConVertDesc,string abckey,string saletaxkey,string date,string image,bool  hangbo,string itemkey1,string description1,string SMSCode,string MFkey,string vendorkey,string CaseDesc,string PackageConvert,string PackageConvertDesc,float Volume,float Weight,int costkey,float Itemlength,float Itemwidth,float Itemheight)
    {
        get_data_to_location();
        int kq = 2;
        con.Open();
        tran = con.BeginTransaction();
        command = new SqlCommand();
        command.Connection = con;
        try
        {
            command.Transaction = tran;
            command.CommandText = ("Insert_INMAST");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@itemkey", itemkey));
            command.Parameters.Add(new SqlParameter("@CombiPack",hangbo));
            command.Parameters.Add(new SqlParameter("@description", description));
            command.Parameters.Add(new SqlParameter("@itemkey1", itemkey1));
            command.Parameters.Add(new SqlParameter("@description1", description1));
            command.Parameters.Add(new SqlParameter("@SMSCode", SMSCode));
            command.Parameters.Add(new SqlParameter("@abckey",abckey));
            command.Parameters.Add(new SqlParameter("@Itemlength",Itemlength));
            command.Parameters.Add(new SqlParameter("@Itemwidth",Itemwidth));
            command.Parameters.Add(new SqlParameter("@Itemheight",Itemheight));
            command.Parameters.Add(new SqlParameter("@saletaxkey", saletaxkey));
            command.Parameters.Add(new SqlParameter("@commodkey",commodkey));
            command.Parameters.Add(new SqlParameter("@inclasskey",inclasskey));
            command.Parameters.Add(new SqlParameter("@productkey",productkey));
            command.Parameters.Add(new SqlParameter("@MFkey", MFkey));
            command.Parameters.Add(new SqlParameter("@vendorkey",vendorkey));
            command.Parameters.Add(new SqlParameter("@purchconvfctr",purchconvfctr));
            command.Parameters.Add(new SqlParameter("@lastcost", lastcost));
            command.Parameters.Add(new SqlParameter("@CaseDesc", CaseDesc));
            command.Parameters.Add(new SqlParameter("@PackageConvert", PackageConvert));
            command.Parameters.Add(new SqlParameter("@Volume",Volume));
            command.Parameters.Add(new SqlParameter("@costkey",costkey));
            command.Parameters.Add(new SqlParameter("@price",price));
            command.Parameters.Add(new SqlParameter("@ConVertDesc",ConVertDesc));
            command.Parameters.Add(new SqlParameter("@PackageConvertDesc",PackageConvertDesc));
            command.Parameters.Add(new SqlParameter("@Weight",Weight));
            command.Parameters.Add(new SqlParameter("@date",DateTime.Now.Date.ToString()));
            command.Parameters.Add(new SqlParameter("@ImgName", image));
            SqlParameter res = new SqlParameter("@res", SqlDbType.Int);
            res.Direction = ParameterDirection.ReturnValue;
            command.Parameters.Add(res);
            command.ExecuteNonQuery();
            kq = Convert.ToInt32(res.Value);
            if (kq == 0)
            {
                for (int i = 0; i < location.Count; i++)
                {
                    insertInloc = new SqlCommand("INSERT_INLOC", con);
                    insertInloc.CommandType = CommandType.StoredProcedure;
                    insertInloc.Transaction = tran;
                    insertInloc.Parameters.Add(new SqlParameter("@itemkey", itemkey));
                    insertInloc.Parameters.Add(new SqlParameter("@location", location[i].ToString()));
                    insertInloc.ExecuteNonQuery();
                }
                tran.Commit();
            }
            else if (kq == 1)
            {
                tran.Rollback();
            }
            else
            {
                tran.Rollback();
            }
        }
        catch (Exception ex)
        {

            tran.Rollback();
            
        }
        finally
        {
           
            con.Close();
        }
        return kq;
    }
}
